package com.thti.mecool.client.util;

import java.io.ByteArrayOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextPaint;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 
 * @author weiguo.ren
 * 
 */
public class StringUtil {
	private static String hexString = "1234567GHJKLMNBV";
	/**
	 * 字符串转换成十六进制值
	 * 
	 * @param bin
	 *            String 我们看到的要转换成十六进制的字符串
	 * @return
	 */
	public static String bin2hex(String bin) {
		char[] digital = "0123456789ABCDEF".toCharArray();
		StringBuffer sb = new StringBuffer("");
		byte[] bs = bin.getBytes();
		int bit;
		for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4;
			sb.append(digital[bit]);
			bit = bs[i] & 0x0f;
			sb.append(digital[bit]);
		}
		return sb.toString();
	}

	/**
	 * 十六进制转换字符串
	 * 
	 * @param hex
	 *            String 十六进制
	 * @return String 转换后的字符串
	 */
	public static String hex2bin(String hex) {
		String digital = "0123456789ABCDEF";
		char[] hex2char = hex.toCharArray();
		byte[] bytes = new byte[hex.length() / 2];
		int temp;
		for (int i = 0; i < bytes.length; i++) {
			temp = digital.indexOf(hex2char[2 * i]) * 16;
			temp += digital.indexOf(hex2char[2 * i + 1]);
			bytes[i] = (byte) (temp & 0xff);
		}
		return new String(bytes);
	}

	public static String toStringVersion(String bytes) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(
				bytes.length() / 2);
		// 将每2位16进制整数组装成一个字节
		for (int i = 0; i < bytes.length(); i += 2)
			baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
					.indexOf(bytes.charAt(i + 1))));

		return new String(baos.toByteArray());
	}

	public static boolean isBlank(String str) {
		int strLen;
		if ((str == null) ||  str.equals("") ||((strLen = str.length()) == 0) || str.equals("null")) {
			return true;
		}
		for (int i = 0; i < strLen; ++i) {
			if (!(Character.isWhitespace(str.charAt(i)))) {
				return false;
			}
		}
		return true;
	}

	public static boolean isNotBlank(String str) {
		return (!(isBlank(str)));
	}

	public static String isString(String str) {
		if (str == null || str.equals("") || str.length()==0 || str.equals("null")) {
			return "";
		} else {
			return str;
		}
	}

	/**
	 * 截取字符串
	 * 
	 * @param s
	 *            要被截取的字符串
	 * @param iLen
	 *            截取的长度
	 * @param hasDot
	 *            是否加...
	 * @return
	 */
	public static String getSubString(String s, int iLen, boolean hasDot) {
		if (StringUtil.isNotBlank(s)) {
			char c = ' ';
			String sAsc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.+-";
			String lsResult = " ";
			int iGetedLen = 0;
			boolean flag = false;
			for (int i = 0; i < s.length(); i++) {
				c = s.charAt(i);
				if (sAsc.indexOf(c) >= 0) {
					lsResult += c;
					iGetedLen += 1;
				} else {
					lsResult += c;
					iGetedLen += 2;
				}
				if (iGetedLen >= iLen) {
					if (i + 1 < s.length()) {
						flag = true;
					}
					break;
				}
			}
			if (iGetedLen <= iLen) {
				if (flag) {
					if (hasDot) {
						return lsResult.trim() + "...";
					} else {
						return lsResult.trim();
					}
				} else {
					return lsResult.trim();
				}
			}
			if (hasDot) {
				lsResult = lsResult + "...";
			}
			return (lsResult.trim());
		} else {
			return "";
		}
	}
	
	/**
	 * 给控件文字设置 粗体
	 * @param view 需要设置 字体粗体 的控件
	 */
	public static void setFakeBoldText(TextView view){
		TextPaint tp = view.getPaint();
		tp.setFakeBoldText(true);
		
	}
	
	/**
	 * 判断字符串是否有特殊字符
	 */
	
	public static boolean isSpecialChar(String str){
		String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？_]";
		Pattern pattern=Pattern.compile(regEx);
		Matcher matcher=pattern.matcher(str);
		return matcher.find();
	}
	
	//最大位数
	public static void lengthFilter(final Context context, final EditText editText, final int max_length, final String err_msg) {
		InputFilter[] filters = new InputFilter[1];
		filters[0] = new InputFilter.LengthFilter(max_length) {
			@Override
			public CharSequence filter(CharSequence source, int start, int end,
			Spanned dest, int dstart, int dend) {
				int destLen = getCharacterNum(dest.toString()); //获取字符个数(一个中文算2个字符)
				int sourceLen =getCharacterNum(source.toString());
				if (destLen + sourceLen > max_length) {
//					Util.sendToast(context, err_msg);
					return "";
				}
				return source;
			}
		};

		editText.setFilters(filters);

	}
	
		public static int getCharacterNum(final String content) {
			if (null == content || "".equals(content)) {
				return 0;
			}else {
				return (content.length() + getChineseNum(content));
			}
		}



		public static int getChineseNum(String s) {
			int num = 0;
			char[] myChar = s.toCharArray();
			for (int i = 0; i < myChar.length; i++) {
				if ((char)(byte)myChar[i] != myChar[i]) {
					num++;
				}
			}
			return num;

		}
		
		/**
		 * 半角 转全角 ：
		 * @param input
		 * @return
		 */
		public static String ToSBC(String input) {
			char[] c = input.toCharArray();
			for (int i = 0; i< c.length; i++) {
				if (c[i] == 32) {
				c[i] = (char) 12288;
				continue;
			}
			if (c[i]< 127)
					c[i] = (char) (c[i] + 65248);
			}
			return new String(c);
		}
		
		/**  
	     * 去除特殊字符或将所有中文标号替换为英文标号  
	     *   
	     * @param str  
	     * @return  
	     */  
	    public static String stringFilter(String str) {   
	        str = str.replaceAll("【", "[").replaceAll("】", "]")   
	                .replaceAll("！", "!").replaceAll("：", ":");// 替换中文标号   
	        String regEx = "[『』]"; // 清除掉特殊字符   
	        Pattern p = Pattern.compile(regEx);   
	        Matcher m = p.matcher(str);   
	        return m.replaceAll("").trim();   
	    }  
	
	
}
